/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */

package org.gokeep.application.config;

import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.gokeep.common.config.GlobalConfig;
import org.gokeep.common.dto.RsaKeyPair;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.io.File;

/**
 * @author xuning
 */
@Configuration
public class InstallProcessConfig implements ApplicationRunner {
    public static final File INSTALL_DATA_DIR = new File(GlobalConfig.HOME_DIR_PATH + "/" + GlobalConfig.CONFIG_FILE_DIR_NAME);
    @Override
    public synchronized void run(ApplicationArguments args) throws Exception {
        start();
        process();
        end();
    }

    /**
     * 安装处理
     */
    private void process() {

    }

    /**
     * 开始
     */
    public void start() {
        // 生成keystore.json
        RsaKeyPair rsaKeyPair = new RsaKeyPair();
        if (rsaKeyPair.existKeyStoreFile()) {
            return;
        }
        rsaKeyPair.autoWriteKeyStoreToConfig();
    }

    /**
     * 结束
     */
    public void end() {

    }
    /**
     * 是否为未安装
     * @return
     */
    @SneakyThrows
    public boolean isUnInstall() {
        return FileUtils.isEmptyDirectory(INSTALL_DATA_DIR);
    }
}
